package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.User;
import com.zzyl.entity.UserRole;
import com.zzyl.mapper.UserMassageMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.UserService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMassageMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 分页查询
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<UserVo> selectByPage(UserDto userDto, Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //查询
        Page<User> users = userMapper.selectByPage(userDto);
        //获取RoleVoIds和RoleLabels
        for (User user : users) {
            Set<String> roleIds =  userRoleMapper.getIdsAndNames(user.getId());
            user.setRoleVoIds(roleIds);
            Set<String> names = userRoleMapper.getNames(user.getId());
            user.setRoleLabels(names);
        }
        PageResponse<UserVo> userVoPageResponse = PageResponse.of(users, UserVo.class);

        return userVoPageResponse;
    }

    /**
     * 添加用户
     * @param userDto
     */
    @Transactional
    @Override
    public void add(UserDto userDto) {
        //添加主表
        User user = BeanUtil.toBean(userDto, User.class);
        //设置初始密码并BCrypt加密
        user.setPassword(BCrypt.hashpw("123456", BCrypt.gensalt()));
        userMapper.insertSelective(user);
        //添加中间表
        Set<String> roleVoIds = userDto.getRoleVoIds();
        ArrayList<UserRole> list = new ArrayList<>();
        for (String roleVoId : roleVoIds) {
            UserRole role = UserRole.builder()
                    .userId( user.getId())
                    .roleId(Long.valueOf(roleVoId))
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            list.add(role);
        }
        //批量插入
        userRoleMapper.batchInsert(list);
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    @Override
    public void enable(Long id, String status) {
        userMapper.enable(id,status);
    }

    /**
     * 编辑
     * @param userDto
     */
    @Transactional
    @Override
    public void update(UserDto userDto) {
        //更新主表
        User user = BeanUtil.toBean(userDto, User.class);
        userMapper.updateByPrimaryKeySelective(user);
        //更新中间表
        //1.删除所有旧数据
        userRoleMapper.deleteByUserId(userDto.getId());
        //2.添加新数据
        Set<String> roleVoIds = userDto.getRoleVoIds();
        ArrayList<UserRole> list = new ArrayList<>();
        for (String roleVoId : roleVoIds) {
            UserRole role = UserRole.builder()
                    .userId( user.getId())
                    .roleId(Long.valueOf(roleVoId))
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            list.add(role);
        }
        userRoleMapper.batchInsert(list);
    }

    /**
     * 删除
     * @param userId
     */
    @Transactional
    @Override
    public void delete(Long userId) {
        //删除主表信息
        userMapper.deleteByPrimaryKey(userId);
        //删除中间表信息
        userRoleMapper.deleteByUserId(userId);
    }

    /**
     * 用户列列表
     * @param userDto
     * @return
     */
    @Override
    public List<UserVo> list(UserDto userDto) {
        User user = BeanUtil.toBean(userDto, User.class);
        List<User> users = userMapper.list(user);
        List<UserVo> userVos = BeanUtil.copyToList(users, UserVo.class);
        return userVos;
    }

    /**
     * 重置密码
     * @param userId
     */
    @Override
    public void resetPassWords(Long userId) {
        //BCrypt加密默认密码(123456)
        String passeord = BCrypt.hashpw("123456", BCrypt.gensalt());
        userMapper.resetPassWords(userId,passeord);
    }


}
